let $api = require('../../config');
Page({
	data:{
		code:0,
		from:'',
		stockInfo:{},
		surplusSearch:0,
		isGreen:false
	},
	/*分享*/
	onShareAppMessage: function (res) {
			let id = wx.getStorageSync('userInfo').user_id;
			if (res.from === 'button') {
					return {
							title: '股票估值查询',
							path: `/pages/index/index?shareId=${id}`,
							imageUrl:'../../images/share-img.png'
					}
			}else{
					return {
							title: '股票估值查询',
							path: `/pages/index/index?shareId=${id}`,
							imageUrl:'../../images/share-img.png'
					}
			}
	},
	/*跳转带来的参数*/
	onLoad: function(option){
		let code = option.code;
		let from = option.from;
		this.setData({
			code,
			from
		})
		this.getStockInfo(code)
		this.getSurplusSearch();
  },
	/*获取股票信息*/
	getStockInfo(code){
		let token = wx.getStorageSync('userInfo').token;
		let self = this;
		wx.request({
				url:$api.api.stockInfo, 
				method:'GET',
				data: {
						token,
						code
				},
				header: {
						'content-type': 'application/json' 
				},
				success: function(res) {
						let data = res.data.data;
						self.setData({
							stockInfo:data,
							isGreen:data.dqjg > data.gz?true:false
						})
				}
		})
	},
	/*事件操作*/
	stockHandler(){
		let {from,code} = this.data;
		let token = wx.getStorageSync('userInfo').token;
		let self = this;
		if(from == 'search'){ /*添加*/
			wx.request({
				url:$api.api.stockAdd, 
				method:'GET',
				data: {
						token,
						code
				},
				header: {
						'content-type': 'application/json' 
				},
				success: function(res) {
						let data = res.data;
						if(data.errCode == 0 || data.msg == '成功'){
							wx.showToast({
									title: '添加成功',
									icon: 'success',
									duration: 2000
							})
							wx.reLaunch({
            		url:`../index/index`
        			})
            }
				}
			})
		}else{
			wx.showModal({
					title: '提示',
					content: '是否确定删除此条自选股',
					success: function(res) {
							if (res.confirm) {
								wx.request({
									url:$api.api.stockDel, 
									method:'GET',
									data: {
											token,
											code
									},
									header: {
											'content-type': 'application/json' 
									},
									success: function(res) {
											let data = res.data;
											if(data.errCode == 0 || data.msg == '成功'){
												wx.showToast({
														title: '删除成功',
														icon: 'success',
														duration: 2000
												})
											}
											wx.reLaunch({
            						url:`../index/index`
        							})
									}
								})
							} else if (res.cancel) {
							}
					}
			})
		}
	},
	toSeach(){
		this.getSurplusSearch((surplusTotal)=>{
			if(surplusTotal > 0){
					wx.navigateTo({
						url:'../search/search'
					})
			}else{
					wx.showModal({
							title: '提示',
							content: '您的查询次数已经用完,是否需要购买会员?',
							success: function(res) {
								if (res.confirm) {
										wx.navigateTo({
											url:'../pay/pay'
										})
								} else if (res.cancel) {
								}
							}
					})
			}
    })
	},
	/*
  * 查询次数
  */
	getSurplusSearch(cb){
			let token = wx.getStorageSync('userInfo').token;
			let self = this;
			wx.request({
					url:$api.api.surplusSearch, 
					method:'GET',
					data: {
							token
					},
					header: {
							'content-type': 'application/json' 
					},
					success: function(res) {
							let surplusTotal = res.data.data.left_query_times;
							self.setData({ 
								surplusSearch:surplusTotal
							})
							cb && cb(surplusTotal)
					}
			})
	}
})